package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    SetmealDishService setmealDishService;

    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> sava(@RequestBody SetmealDto setmealDto) {
        setmealService.addSetmeal(setmealDto);
        return R.success("保存套餐成功");
    }

    @GetMapping("/page")
    public R page(int page, int pageSize, String name) {
        Map map = new HashMap();

        Page<Setmeal> pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();

        qw.like(name != null, Setmeal::getName, name);

        setmealService.page(pageInfo, qw);

        long total = pageInfo.getTotal();

        map.put("total", total);

        List<Setmeal> records = pageInfo.getRecords();
        ArrayList<SetmealDto> list = new ArrayList<>();

        for (Setmeal setmeal : records) {
            Long id = setmeal.getCategoryId();
            Category byId = categoryService.getById(id);
            SetmealDto setmealDto = new SetmealDto();
            setmealDto.setCategoryName(byId.getName());
            BeanUtils.copyProperties(setmeal, setmealDto);
            list.add(setmealDto);
        }
        map.put("records", list);
        return R.success(map);
    }

    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids) {
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }

    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable int status,Long[] ids) {
        LambdaUpdateWrapper<Setmeal> qw = new LambdaUpdateWrapper<>();
        qw.set(Setmeal::getStatus,status);
        qw.in(Setmeal::getId,ids);
        setmealService.update(qw);
        return R.success("操作成功");
    }

    @GetMapping("/{id}")
    public R<SetmealDto> getOne(@PathVariable Long id){
        Setmeal one = setmealService.getById(id);
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,one.getId());
        List<SetmealDish> list = setmealDishService.list(qw);
        SetmealDto setmealDto = new SetmealDto();
        setmealDto.setSetmealDishes(list);
        BeanUtils.copyProperties(one,setmealDto);
        return R.success(setmealDto);
    }
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateSetmeal(setmealDto);
        return R.success("修改套餐成功") ;
    }


    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R<List<Setmeal>> list ( Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(setmeal.getCategoryId()!= null,Setmeal::getCategoryId,setmeal.getCategoryId());
        qw.eq(setmeal.getStatus()!= null,Setmeal::getStatus,setmeal.getStatus());
        qw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(qw);
        return R.success(list);
    }
}
